
export default class DateUtil {

    public static formatNumStr(num: number) {
        let str = "" + num;
        if (num < 10) {
            str = "0" + num;
        }
        return str;
    }

    //格式化当前时间
    public static formateNow(): string {
        let date: Date = new Date(); //后端返回的时间戳是秒
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + ":" + date.getMilliseconds();
    }

    public static formateYearMonthDayStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    }

    public static formateMonthDayStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return (date.getMonth() + 1) + "月" + date.getDate() + "日";
    }

    //  timestamp:1453094034000  2018-1-31 19:53:44
    //根据时间戳返回 2018-1-31 19:53:44 
    public static formatDateStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + this.formatNumStr(date.getHours()) + ":" + this.formatNumStr(date.getMinutes()) + ":" + this.formatNumStr(date.getSeconds());
    }

    //  timestamp:1453094034000  2018-1-31-19-53-44 
    //根据时间戳返回 2018-1-31-19-53-44 
    public static formatDateStr2(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + this.formatNumStr(date.getHours()) + "-" + this.formatNumStr(date.getMinutes()) + "-" + this.formatNumStr(date.getSeconds());
    }

    //  timestamp:1453094034000  2018-1-31
    //根据时间戳返回 2018-1-31
    public static formatDateStr3(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    }

    //  timestamp:1453094034000  
    //根据时间戳返回 19:53
    public static formatHourMinStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return this.formatNumStr(date.getHours()) + ":" + this.formatNumStr(date.getMinutes());
    }

    //  timestamp:1453094034000  
    //根据时间戳返回 19:53:11
    public static formatHourMinSecondStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return this.formatNumStr(date.getHours()) + ":" + this.formatNumStr(date.getMinutes()) + ":" + this.formatNumStr(date.getSeconds());
    }

    public static now(): number {
        let date: Date = new Date();
        return date.getTime();
    }

    public static betweenTime(startTime: number, endTime: number) {
        let date: Date = new Date();
        if (date.getTime() >= startTime && date.getTime() <= endTime) {
            return true;
        }
        return false;
    }

    //根据时间戳返回 19:53:11
    public static formatLeftTime2(timestamp: number) {
        let result: string = "";
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        result = this.formatNumStr(hour) + ":" + this.formatNumStr(min) + ":" + this.formatNumStr(second)
        return result
    }

    //根据时间戳返回 1天19:53:11
    public static formatLeftTime(timestamp: number) {
        let result: string = "";
        let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        result = day + "天" + this.formatNumStr(hour) + ":" + this.formatNumStr(min) + ":" + this.formatNumStr(second)
        return result
    }


    //根据时间戳返回 1天13小时02分钟
    public static formatLeftTime3(timestamp: number) {
        let result: string = "";
        let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        if (day > 0) {
            result = day + "天" + this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        } else {
            result = this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        }
        return result
    }

    //根据时间戳返回 1天13小时02分钟
    public static formatLeftTime4(timestamp: number) {
        let result: string = "";
        let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        if (day > 0) {
            result = day + "天" + this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        } else {
            result = this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        }
        return result
    }

    public static isToday(dateTime: number): boolean {
        let nowDate: Date = new Date();
        let checkDate: Date = new Date(dateTime);
        if (checkDate.getFullYear() == nowDate.getFullYear() && checkDate.getMonth() == nowDate.getMonth() && checkDate.getDate() == nowDate.getDate()) {
            return true;
        }
        return false;
    }

    //2020-04-29 10:00:00 根据时间string返回date
    public static getDateByStr(dateStr: string) {
        let arr: Array<string> = dateStr.split(" ");
        let arr2: Array<string> = arr[0].split("-");
        let arr3: Array<string> = arr[1].split(":");
        let year: number = parseInt(arr2[0]);
        let month: number = parseInt(arr2[1]) - 1;
        let day: number = parseInt(arr2[2]);
        let hour: number = parseInt(arr3[0]);
        let min: number = parseInt(arr3[1]);
        let second: number = parseInt(arr3[2]);
        let date: Date = new Date(year, month, day, hour, min, second);
        // Logger.log("getDateByStr==", dateStr, arr, arr2, arr3, year, month, day, hour, min, second, date);
        return date;

    }

    public static getPassByDay2Day(startTime:number, endTime:number){
        let startDate:Date = new Date(startTime);
        let endDate:Date = new Date(endTime);
        startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())
        endDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())
        startTime = startDate.getTime();
        endTime = endDate.getTime();
        let passTime: number = endTime - startTime;
        if (passTime < 0) {
            passTime = 0;
        }
        let day: number = Math.floor(passTime / (1000 * 60 * 60 * 24))
        return day;
    }

    //返回过去几天
    public static getPassDay(checkTime: number) {
        let now: number = DateUtil.now();
        let passTime: number = now - checkTime;
        if (passTime < 0) {
            passTime = 0;
        }
        let day: number = Math.floor(passTime / (1000 * 60 * 60 * 24))
        return day;
    }

    public static getLeftDay(checkTime: number) {
        // // let today: Date = new Date(ServerTime.getServerTime());
        // let today: Date = new Date(ServerTime.getServerTime());
        // today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
        // let checkDate: Date = new Date(checkTime);
        // checkDate = new Date(checkDate.getFullYear(), checkDate.getMonth(), checkDate.getDate());
        // let leftTime: number = checkTime - ServerTime.getServerTime();
        // Logger.log("getLeftDay=====",checkTime, today, checkDate, leftTime);
        // if (leftTime < 0) {
        //     leftTime = 0;
        // }
        // let day: number = Math.floor(leftTime / (1000 * 60 * 60 * 24))
        // Logger.log("getLeftDay==22===", leftTime, leftTime / (1000 * 60 * 60 * 24));
        // return day;
    }

    public static getDay(checkTime: number) {
        let day: number = Math.floor(checkTime / (1000 * 60 * 60 * 24))
        return day;
    }

}
